package com.sun.designpatternsdemo.state

import android.os.Bundle
import android.util.Log
import com.sun.designpatternsdemo.R
import com.sun.designpatternsdemo.app.AppConstant
import com.sun.designpatternsdemo.app.EMTagHandler
import com.sun.designpatternsdemo.base.BaseActivity
import com.sun.designpatternsdemo.databinding.ActivityStateBinding
import com.sun.designpatternsdemo.state.bean.VendingMachine
import com.sun.designpatternsdemo.state.bean.VendingMachineBetter

/**
 * 状态模式
 * 定义：允许对象在内部状态改变时改变它的行为，对象看起来好像修改了它的类。
 * 定义又开始模糊了，理一下，当对象的内部状态改变时，它的行为跟随状态的改变而改变了，看起来好像重新初始化了一个类似的。
 */
class StateActivity : BaseActivity<ActivityStateBinding>() {


    override fun layoutId(): Int = R.layout.activity_state

    override fun initView(savedInstanceState: Bundle?) {
        title = "状态模式"
        mDataBind.apply {
            tvDefine.text = EMTagHandler.fromHtml(AppConstant.STATE_DEFINE)
            btFacadeOld.text = "最初实现待改进"
            btFacadeBetter.text = "改进过的售货机"
            btFacadeOld.setOnClickListener {
                val vendingMachine = VendingMachine(3)
                vendingMachine.apply {
                    insertMoney()
                    turnCrank()
                    Log.e("测试:", "----------------------")
                    insertMoney()
                    turnCrank()
                    Log.e("测试:", "----------------------")
                    insertMoney()
                    turnCrank()
                    Log.e("压力测试:", "----------------------")
                    insertMoney()
                    insertMoney()
                    turnCrank()
                    turnCrank()
                    backMoney()
                    turnCrank()
                }
            }
            btFacadeBetter.setOnClickListener {
                val machine = VendingMachineBetter(4)
                machine.apply {
                    // 正常: 投币  退币  摇杆
                    insertMoney()
                    turnCrank()
                    insertMoney()
                    turnCrank()
                    insertMoney()
                    turnCrank()
                    insertMoney()
                    turnCrank()
                    Log.e("压力测试:", "----------------------")
                    insertMoney()
                    insertMoney()
                    insertMoney()
                    backMoney()
                    backMoney()
                    backMoney()
                    turnCrank()
                    turnCrank()
                    turnCrank()
                }
            }
        }
    }
}